from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.db.models import Q
from django.http import JsonResponse, QueryDict
from rest_framework import viewsets, routers
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.renderers import JSONRenderer
from rest_framework.decorators import *
from rest_framework.pagination import PageNumberPagination
from core.service import cmd
class notify(APIView):
        #todo
        #def {method name}(self, request, format=None):
        def get(self,request, format= None):
            if 'command' not in request.query_params:
                return JsonResponse({"state":-1, "msg":"miss request.query_params 'command' parameter."})
            else:
                # 后期可以做工厂模式
                command= request.query_params['command']
                #接收到系统加载成功执行
                try:
                    args=tuple()
                    kwargs= {}
                    state,data= cmd.todo(command,request,*args,**kwargs)
                    if state:
                        return JsonResponse({"state":200, "msg":"run cmd success."})
                    else:
                        return JsonResponse({"state":-1,"msg":data})
                except Exception as e:
                    return JsonResponse({"state":-1, "msg":e.args})
